<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<el-radio-group v-model="viewType" size="small">
						<el-radio-button label="list-view">
							<span class="el-icon-c-scale-to-original"></span>
							<span>列表</span>
						</el-radio-button>
						<el-radio-button label="chart-view">
							<span class="el-icon-s-data"></span>
							<span>图表</span>
						</el-radio-button>
					</el-radio-group>
					<!--<jk-report-button :permission-prefix="prefix" class="margin-5-0" :loading="exportLoading" @click="onReportBtn"></jk-report-button>-->
				</div>
				<div class="flex-end-center-wrap">
					<el-tag effect="plain" size="default" type="primary" class="item-gap">
						<span class="collect-current-time el-icon-time"></span>
						<span class="collect-current-time">{{ currentTime }}</span>
					</el-tag>
					<jk-select v-model="searchParams.workshopId" :option-list="workshopList" clearable placeholder="区域" size="small" class="search-bar-item-size1 margin-5-0 item-gap" @change="onChange">
					</jk-select>
					<el-select v-model="searchParams.processCode" clearable placeholder="工序" size="small" class="search-bar-item-size1 margin-5-0 item-gap">
						<el-option v-for="item in processList" :key="item.id" :label="item.name" :value="item.code" />
					</el-select>
					<!--					<el-select v-model="searchParams.shiftId" clearable placeholder="班次" size="small" class="search-bar-item-size1 margin-5-0 item-gap">-->
					<!--						<el-option v-for="item in shiftList" :key="item.id" :label="item.name" :value="item.id" />-->
					<!--					</el-select>-->
					<jk-search-input v-model="searchParams.deviceRecordNumber" placeholder="设备编号" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>
		<jk-card>
			<component
				:is="viewType"
				:table-column-list="tableColumnList"
				:table-data="tableData"
				:height="tableHeight + 20"
			></component>
			<!-- 分页组件 -->
			<!--			<footer ref="box2" class="flex-end-center margin-top-10">-->
			<!--				<jk-page-->
			<!--					:page-index="searchParams.pageNo"-->
			<!--					:page-sizes="searchParams.pageSizes"-->
			<!--					:page-size="searchParams.pageSize"-->
			<!--					:total="searchParams.count"-->
			<!--					@on-change="onPage"-->
			<!--				/>-->
			<!--			</footer>-->
		</jk-card>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { formatDate, formatTime, pageConfig } from '/src/utils/jk/jk-tool';
    import permissionMixin from '../../../mixins/permission-mixin';
    import listView from './components/list-view';
    import chartView from './components/chart-view';
    import scheduleShift from './scheduleShift';
    import {
        iotCollectDataCountPage,
        productionMachineShiftOutputCurrentPage
    } from '../../../api/production/frontGroupYieldStatistics';
    import { spinProcessPage } from '../../../api/technology/process';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import { getInfo } from '@/api/login';
    import { workforceManagementShiftGetByTime, workforcemanagementShiftPage } from '@/api/schedule/shift';
    export default {
        name: 'ScheduleShiftProductionReport',
        mixins: [permissionMixin, calHeight, getMenuColumn, scheduleShift],
        components: {
            listView,
            chartView
        },
        data() {
            return {
                prefix: 'iot:collectTask',
                viewType: 'list-view',
                timer: null,
                timer2: null,
                processList: [],
                workshopList: [],
                shiftList: [],
                currentTime: '',
                dataId: null,
                tableLoading: true,
                exportLoading: false,
                tableData: [],
                productList: [],
                searchParams: {
                    count: 0,
                    pageNo: 1,
                    pageSize: 500,
                    deviceRecordNumber: null,
                    processCode: null,
                    // shiftId: '',
                    workshopId: null
                }
            };
        },
        methods: {
            onPage(e, size) {
                this.searchParams.pageNo = e;
                this.searchParams.pageSize = size;
                this.getListRequest();
            },
            runScheduleTask() {
                this.getListRequest();
                this.timer = setInterval(() => {
                    this.getListRequest();
                }, 5000);
                this.timer2 = setInterval(() => {
                    this.currentTime = formatTime(Date.now());
                }, 1000);
            },
            getProcessListRequest() {
                return spinProcessPage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.processList = res.data.res;
                    }
                });
            },
            insetProductInfo() {
                this.tableData = this.tableData.map(x => {
                    return x;
                });
            },
            onChange() {
            },
            // 获取所有班次
            getShiftListRequest() {
                const params = JSON.parse(JSON.stringify(this.searchParams));
                delete params.pageNo;
                delete params.pageSize;
                // delete params.shiftId;
                delete params.count;
                delete params.yarnCount;
                delete params.belongDate;
                delete params.processCode;
                params.auditState = 1;
                return workforcemanagementShiftPage(params).then(res => {
                    console.log(res.data.res);
                    if (res.data.status === 200) {
                        this.shiftList = res.data.res || [];
                    }
                });
            },
            // 当前班次信息
            getScheduleShift() {
                const params = JSON.parse(JSON.stringify(this.searchParams));
                delete params.pageNo;
                delete params.pageSize;
                // delete params.shiftId;
                delete params.count;
                delete params.yarnCount;
                delete params.belongDate;
                delete params.processCode;
                params.belongTime = formatTime(Date.now());
                return workforceManagementShiftGetByTime(params).then(res => {
                    if (res.data.status === 200) {
                        // this.$set(this.searchParams, 'shiftId', res.data.res.currentShiftInfo && res.data.res.currentShiftInfo.id ? res.data.res.currentShiftInfo.id : null);
                    }
                });
            },
            async getDependentData() {
                await this.getMenuColumnListRequest();
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                if (!this.searchParams.workshopId) {
                    this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                }
                await this.getScheduleShift();
                // await this.getShiftListRequest();
                await this.getProcessListRequest();
                this.runScheduleTask();
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                return productionMachineShiftOutputCurrentPage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res ? res.data.res.map(x => {
                            return x;
                        }) : [];
                        this.insetProductInfo();
                        this.searchParams.count = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.searchParams.count = 1;
                this.getListRequest();
            },
            /** 导出按钮操作 */
            onReportBtn() {
                const params = { ...this.searchParams };
                params.pageNo = null;
                params.pageSize = null;
                /* this.$modal.confirm('是否确认导出所有数据数据项?').then(() => {
            this.exportLoading = true;
            return iotCollectTaskExportExcel(params);
        }).then(res => {
            if (res.data.status === 200) {
                this.$download.excel(res, `${this.$route.name}.xls`);
                this.exportLoading = false;
            }
        }).catch(() => {});*/
            }
        },
        beforeRouteEnter(to, from, next) {
            next((_this) => {
                _this.currentTime = formatTime(Date.now());
                _this.getDependentData();
            });
        },
        beforeRouteLeave(to, from, next) {
            clearInterval(this.timer);
            clearInterval(this.timer2);
            next();
        },
        mounted() {

        }
    };
</script>
<style scoped>
.collect-current-time {
  margin-right: 4px;
  font-size: 16px;
}
</style>
